/*
 * cli_binding.c
 *
 *  Created on: Jul 7, 2023
 *      Author: NeusAap
 */

#include <stdio.h>

#include "cli_setup.h"
#include "cli_binding.h"
#include "../cm_backtrace/fault_test.h"
#include "netconf/netconf.h"

void reboot(EmbeddedCli *cli, char *args, void *context) {
    cli_printf("rebooting ...");
    NVIC_SystemReset();
}

void onLed(EmbeddedCli *cli, char *args, void *context) {
    const char *arg1 = embeddedCliGetToken(args, 1);
    const char *arg2 = embeddedCliGetToken(args, 2);
    if (arg1 == NULL || arg2 == NULL) {
        cli_printf("usage: get-led [arg1] [arg2]");
        return;
    }
    // Make sure to check if 'args' != NULL, printf's '%s' formatting does not like a null pointer.
    cli_printf("LED with args: %s and %s", arg1, arg2);
}

void switch_dhcp_mode(EmbeddedCli *cli, char *args, void *context) {
    const char *arg1 = embeddedCliGetToken(args, 1);

    char *end_ptr = NULL;
    uint32_t dhcp_enabled = strtoul(arg1, &end_ptr, 10);
    cli_printf("%s DHCP", dhcp_enabled ? "enable" : "disable");

    netconf_switch_mode(dhcp_enabled);
}

void initCliBinding() {
    // Define bindings as local variables, so we don't waste static memory

    // Command binding for the clear command
    CliCommandBinding clear_bindings[] = {{
                                                  .name = "reboot",
                                                  .help = "reboot",
                                                  .tokenizeArgs = true,
                                                  .context = NULL,
                                                  .binding = reboot
                                          },
                                          {
                                                  .name = "dhcp",
                                                  .help = "dhcp [0 | 1]",
                                                  .tokenizeArgs = true,
                                                  .context = NULL,
                                                  .binding = switch_dhcp_mode
                                          },
    };

    EmbeddedCli *cli = getCliPointer();

    for (int32_t i = 0; i < sizeof(clear_bindings) / sizeof(clear_bindings[0]); i++) {
        embeddedCliAddBinding(cli, clear_bindings[i]);
    }

}
